package com.lxl.hadoop.selfinputformat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * @Author lixilin
 * @Date 2021/12/25 10:50
 * @Description
 */
public class MyRecordReader extends RecordReader<Text, BytesWritable> {
    FileSplit split;
    Configuration configuration;
    Text k = new Text();
    BytesWritable v = new BytesWritable();
    // 每个小文件会创建一个RecordReader对象
    boolean isNew = true;

    public void initialize(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {
        this.split = (FileSplit) split;
        configuration = context.getConfiguration();

    }

    // 核心业务逻辑
    public boolean nextKeyValue() throws IOException, InterruptedException {
        if (isNew) {
            FSDataInputStream is = null;
            try {

                // 获取fs
                Path path = split.getPath();
                FileSystem fs = path.getFileSystem(configuration);
                // 获取输入流
                is = fs.open(path);
                //封装v
                byte[] buf = new byte[(int) split.getLength()];
                IOUtils.readFully(is, buf, 0, buf.length);
                v.set(buf, 0, buf.length);
                //封装k
                k.set(path.toString());
            } finally {
                // 关闭资源
                IOUtils.closeStream(is);
            }
            isNew = false;
            return true;
        }
        return false;
    }

    public Text getCurrentKey() throws IOException, InterruptedException {
        return k;
    }

    public BytesWritable getCurrentValue() throws IOException, InterruptedException {
        return v;
    }

    public float getProgress() throws IOException, InterruptedException {
        return 0;
    }

    public void close() throws IOException {

    }
}
